#include "TripWire.h"
#include "Physics.h"

#include "Events\CSGD_EventSystem.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "Camera.h"
#include "Time.h"

TripWire::TripWire(void)
{
	hasTriggered = false;
	linecastFrequency = 3;
	objectID = "Trip Wire";
	direction.fX = 0;
	direction.fY = -1;
	//set in levelmanager upon creation
	start.fX = 0;
	start.fY = 0;
}


TripWire::~TripWire(void)
{

}


void TripWire::Update(float deltaTime)
{
	//Linecast
	if( Time::GetInstance().TotalFrames() % linecastFrequency == 0)
	{
		LinecastInfo info;

		if( Physics::LineCast(info, 5000,start,direction, GetObjectID().c_str()))
		{
			distance = info.Distance;
			//assume cardinal directions
			if(direction.fY == -1)
				distance = -distance;
			if(direction.fX == -1)
				distance = -distance;

			if(info.HitObject->GetObjectID() == "Player")
			{
				hasTriggered = true;
				CSGD_EventSystem::GetInstance()->SendEvent("Trip Wire Activate",(void*)0,(void*)info.HitObject, (void*)this);
			}
			else if ( info.HitObject->GetObjectID()  == "Wooden Box")
			{
				hasTriggered = true;
				CSGD_EventSystem::GetInstance()->SendEvent("Trip_Wire_Activate",(void*)0,(void*)info.HitObject, (void*)this);
			}
			else if (info.HitObject->GetObjectID()  == "Metal Box")
			{
				hasTriggered = true;
				CSGD_EventSystem::GetInstance()->SendEvent("Trip_Wire_Activate",(void*)0,(void*)info.HitObject, (void*)this);
			}
			else if (info.HitObject->GetObjectID()  == "Trip Wire")
			{
				//TODO: Make only one of the two linecast
			}
		}
	}
}
void TripWire::Render(void) const
{
	//Draw this end
	if(imageID != -1)
	{
		CSGD_TextureManager::GetInstance()->Draw(imageID, 
			(int)(positionX - Camera::GetInstance()->OffsetX()),
			(int)(positionY - Camera::GetInstance()->OffsetY()));
	}
	if(!hasTriggered)
	{
		//RECT tempBox = hitBox;
		//tempBox.top -= Camera::GetInstance()->OffsetY();
		//tempBox.bottom -= Camera::GetInstance()->OffsetY();
		//tempBox.left -= Camera::GetInstance()->OffsetX();
		//tempBox.right -= Camera::GetInstance()->OffsetX();
		//CSGD_Direct3D::GetInstance()->DrawRect(hitBox,D3DCOLOR_ARGB(255,255,0,0));


		//Draw 'wire'

		if(direction.fX == 0)
		{
			if(direction.fY == -1)
			{
				CSGD_Direct3D::GetInstance()->DrawLine((int)(start.fX - Camera::GetInstance()->OffsetX()), (int)(start.fY - Camera::GetInstance()->OffsetY()), (int)(start.fX - Camera::GetInstance()->OffsetX()),
							(int)(start.fY + distance - Camera::GetInstance()->OffsetY()), D3DCOLOR_ARGB(255,0,255,0));
			}
			else if (direction.fX == 1)
			{
				CSGD_Direct3D::GetInstance()->DrawLine( (int)(start.fX - Camera::GetInstance()->OffsetX()), (int)(start.fY - Camera::GetInstance()->OffsetY()), (int)(start.fX - Camera::GetInstance()->OffsetX()),
							(int)(start.fY + distance - Camera::GetInstance()->OffsetY(), D3DCOLOR_ARGB(255,0,255,0)));
			}
		}
		else 
		{
			if(direction.fX == 1)
			{
				CSGD_Direct3D::GetInstance()->DrawLine((int)(start.fX - Camera::GetInstance()->OffsetX()), (int)(start.fY - Camera::GetInstance()->OffsetY()), (int)(start.fX + distance - Camera::GetInstance()->OffsetX()),
							(int)(start.fY - Camera::GetInstance()->OffsetY()), D3DCOLOR_ARGB(255,0,255,0));
			}
			else if(direction.fX == -1)
			{
				CSGD_Direct3D::GetInstance()->DrawLine( (int)(start.fX - Camera::GetInstance()->OffsetX()), (int)(start.fY - Camera::GetInstance()->OffsetY()), (int)(start.fX + distance - Camera::GetInstance()->OffsetX()),
							(int)(start.fY - Camera::GetInstance()->OffsetY(), D3DCOLOR_ARGB(255,0,255,0)));
			}
		}

	}
}

void TripWire::HandleCollision(GameObject& other, CollisionInfo& info)
{
	//if(other.GetObjectID() == "Player")
	//{
	//	hasTriggered = true;
	//	CSGD_EventSystem::GetInstance()->SendEvent("Trip_Wire_Activate",(void*)0,(void*)0, (void*)this);
	//}
	//else if ( other.GetObjectID() == "Wooden Box")
	//{
	//	hasTriggered = true;
	//	CSGD_EventSystem::GetInstance()->SendEvent("Trip_Wire_Activate",(void*)0,(void*)0, (void*)this);
	//}
	//else if (other.GetObjectID() == "Metal Box")
	//{
	//	hasTriggered = true;
	//	CSGD_EventSystem::GetInstance()->SendEvent("Trip_Wire_Activate",(void*)0,(void*)0, (void*)this);
	//}
}